// 字符串
package strings

import (
	"fmt"
	"math/rand"
	"time"
	"unsafe"

	"gitee.com/copylian/lian-go.git/helper/hash"
)

// GetRand 获取随机字符串
func GetRand(l int) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	result := make([]byte, l)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := range result {
		result[i] = str[r.Intn(61)]
	}
	return fmt.Sprintf("%v", string(result))
}

// CreatePassword 生成密码
func CreatePassword(p, s string) string {
	return hash.MD5(p + s)
}

// StrToBytes string转bytes
func StrToBytes(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}

// BytesToStr bytes转string
func BytesToStr(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}
